﻿package mlc.avm2;
import flash.system.System;
import hsl.haxe.data.mathematics.Point;
import mlc.avm2.visualizing.AVM2ConsoleVisualizer;
import mlc.haxe.Console;

/**
 * A typical console that can be used in AVM2 websites, applications and games. The console can be used for many different
 * things. It can be used, for instance, to store and show error and debug messages. It can also be used to change the state of
 * an application or game at runtime. The AVM2 console has a visual representation that can appear right away, or appears when
 * the user presses the tilde key while holding the control key down, or the C key while holding both the control and the shift
 * key down. The visual representation adds itself to the stage right away, or when the user uses such a shortcut, depending on
 * the configuration.
 */
class AVM2Console extends Console {
	/**
	 * Creates a new AVM2 console. The original stage dimensions should be passed. If they are not passed, they will be guessed.
	 * Such guessed values can be wrong, and wrong values could result in visual glitches. If you set the initiallyOpened
	 * argument to true, the console will be opened right away. If you set that argument to false (the default), the user can
	 * open the console by pressing the tilde key while holding the control key down, or the C key while holding both the control
	 * and the shift key down. You can choose how the output that comes from the console before the visual representation has
	 * been opened is handled. If you set the write to buffer argument to true (the default), such output will be stored in a
	 * buffer and will appear in the visual representation when it first opens. If you set that property to false, such output
	 * will be ignored: the user will have to open the visual representation before output from the console appears in it.
	 */
	public function new(?originalStageDimensions:Point, initiallyOpened:Bool = false, writeToBuffer:Bool = true):Void {
		var consoleVisualizer:AVM2ConsoleVisualizer = new AVM2ConsoleVisualizer(originalStageDimensions, initiallyOpened, writeToBuffer);
		super(consoleVisualizer.outputHandler, consoleVisualizer.inputSource);
		// Add the "memory" command, that shows the current memory usage of the application.
		addVoidCommand("memory", traceMemoryUsage);
		addAlias("mem", "memory");
	}
	public function traceMemoryUsage():Void {
		write("Memory in use: ", 0x8D8DFF, false);
		write(Math.round(System.totalMemory / 1024 / 1024 * 100) / 100 + " MiB", 0x62FF4C, true);
	}
}